package online.inote.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import online.inote.common.base.ESController;
import online.inote.common.base.Page;
import online.inote.common.utils.StringUtils;
import online.inote.model.ArticleEntity;
import online.inote.service.ArticleService;

@RestController
@RequestMapping(value = "article")
@Api(tags = {"文章接口"}, value = "ArticleController")
public class ArticleController extends ESController<ArticleEntity, String> {

	@Autowired
	private ArticleService articleService;
	
	/**
	 * <p>
	 * Desc: ES分页查询
	 * </p>
	 *
	 * @author XQF SUI
	 * @created 2018年8月20日 上午11:30:43
	 * @param page
	 * @param article
	 * @return
	 */
	@ApiOperation(value = "查询用户", notes = "根据用户ID查询用户")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "Integer", paramType = "query"), 
		@ApiImplicitParam(name = "rows", value = "每页条数", required = true, dataType = "Integer", paramType = "query"),
		@ApiImplicitParam(name = "title", value = "标题", dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "author", value = "作者姓名", dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", paramType = "query"),
		@ApiImplicitParam(name = "summary", value = "简介信息", dataType = "String", paramType = "query")
	})
	@RequestMapping(value = "selectESPage", method = RequestMethod.POST)
	public Page<ArticleEntity> selectESPage(Page<ArticleEntity> page, ArticleEntity article) {
		Criteria criteria = new Criteria();
		
		if (StringUtils.isNotBlank(article.getTitle())) {
			criteria.and(new Criteria().and("title").is(article.getTitle()));
		}
		
		if (StringUtils.isNotBlank(article.getAuthor())) {
			criteria.and(new Criteria().and("author").is(article.getAuthor()));
		}
		
		if (article.getCreateTime() != null) {
			criteria.and(new Criteria().and("createTime").greaterThanEqual(article.getCreateTime().getTime()));
		}
		
		if (StringUtils.isNotBlank(article.getSummary())) {
			criteria.and(new Criteria().and("summary").contains(article.getSummary()));
		}
		
		criteria.and(new Criteria().and("id").in(idList()));
		
		articleService.selectESPage(page, criteria, new Sort(Direction.DESC, "createTime"));
		
		return page;
	}
	
	private List<String> idList() {
		List<String> list = new ArrayList<>();
		list.add("7be2c747-d902-11e7-982e-2089842fe919");
		list.add("7bf52e62-d902-11e7-982e-2089842fe919");
		return list;
	}
}